import { _decorator, Component, Node, tween, v3, randomRangeInt } from 'cc';
import { Card } from './Card';
import { CardGroupParent } from './CardGroupParent';
import GameAllData from './GameAllData';
import { SoltaireGame } from './SoltaireGame';
import GameManager from './tool/GameManager';
import { SoundEffect } from './tool/SoundEffect';
const { ccclass, property } = _decorator;
/**
 * Ai玩家
 */
@ccclass('PlayGroupAi')
export class PlayGroupAi extends CardGroupParent {

    start() {

    }

    //位置排序
    PositionSort()
    {
        for (let i = 0; i < this.cards.length; i++) {
            const element = this.cards[i];
            let end=v3(40+15*i,0,0);
            tween(element.node).to(0.5, {position:end})
            .start()

            tween(element.node).delay(0.25).call(()=>{
                element.node.setSiblingIndex(i);
            }).start()
            //element.node.position=end;
            element.isTouch=false;
        }
    }

    AiInterchangeSelect(num:number)
    {
        for (let i = 0; i < num; i++) {
            this.interchangeCards.push(this.cards[i]);
        }

    }

    SelectCards(card:Card,isadd:boolean)
    {

    }

    //ai选择出分
    AiSelectChuScore()
    {
        let cardClone:Card[]=[];
        for (let i = 0; i < this.cards.length; i++) {
            const element = this.cards[i];
            cardClone.push(element);
        }
        cardClone.sort((a,b)=>{
            if (a.cardData==501) {
                return 1;
            } else 
            {
                if (a.cardNumber>b.cardNumber) {
                    return 1;
                } else if (a.cardNumber==b.cardNumber){
                    if (a.cardColor>b.cardColor) {
                        return 1;
                    } else {
                        return -1;
                    }
                }else
                {
                    return -1;
                }
            }
        })

        let havePairs=false;//对子
        for (let i = 0; i < 4; i++) {
            const card = cardClone[i];
            const cardNext = cardClone[i+1];
            if (card.cardNumber==cardNext.cardNumber) {
                havePairs=true;
                break;
            }
        }

        let haveStraigh=false;//有顺子
        for (let i = 0; i < 3; i++) {
            const card = cardClone[i];
            const cardNext = cardClone[i+1];
            const cardNext2 = cardClone[i+2];
            if ((card.cardNumber+2)==(cardNext.cardNumber+1)&&(card.cardNumber+2)==cardNext2.cardNumber) {
                haveStraigh=true;
                break;
            }
        }

        let haveSameColor=false;//有顺子
        let sameColor=[0,0,0,0];//有花色
        for (let i = 0; i < 5; i++) {
            const card = cardClone[i];
            sameColor[card.cardColor-1]++;
        }

        for (let i = 0; i < 4; i++) {
            if (sameColor[i]>=3) {
                haveSameColor=true;
                break;
            }
        }

        //
        if (havePairs||haveStraigh||haveSameColor) {
            this.scheduleOnce(()=>{
                let rd= randomRangeInt(0,100);
                if (rd<50) {
                    if (GameAllData.Instance.lastPlayUpScore==0) {
                        this.SelectGen(100*GameManager.Instance.difficult);
                    } else {
                        this.SelectGen(GameAllData.Instance.lastPlayUpScore);
                    }
                }else
                {
                    if (GameAllData.Instance.lastPlayUpScore==0) {
                        this.SelectAdd100(200*GameManager.Instance.difficult);
                    } else {
                        this.SelectAdd100(GameAllData.Instance.lastPlayUpScore+100*GameManager.Instance.difficult);
                    }
                }
            },0.5);
        } else 
        {//牌不好
            this.scheduleOnce(()=>{
                this.SelectPass();
            },0.5);
        }

        this.scheduleOnce(()=>{
            SoltaireGame.instance.NaxtTakeTurnsPlayCard(this.groupId,0.2);
        },1.5);
    }

    //ai选择交换
    AiSelectInterchangeCard()
    {
        this.selectCards=[];
        let type= this.GetPokerHands();
        if (type<=5) 
        {//更换
            SoltaireGame.instance.NaxtTakeTurnsPlayCard(this.groupId,2);
            return;
        }

        let cardClone:Card[]=this.GetSortCards();
        let cardNum=[0,0,0,0,0,0,0,0,0,0,0,0,0]; 
        for (let i = 0; i < 5; i++) {
            const card = cardClone[i];
            cardNum[card.cardNumber-1]++;
        }

        if (type==6) 
        {//三条
            for (let i = 0; i < cardNum.length; i++) {
                if (cardNum[i]==3) {
                    for (let j = 0; j < cardClone.length; j++) {
                        if (cardClone[j].cardNumber!=(i+1)) {
                            this.selectCards.push(cardClone[j]);
                        }
                    }
                }
            }
        }else
        {
            let haveSameColor=-1;//
            let sameColor=[0,0,0,0];//有花色
            for (let i = 0; i < 5; i++) {
                const card = cardClone[i];
                sameColor[card.cardColor-1]++;
            }
    
            for (let i = 0; i < 4; i++) {
                if (sameColor[i]>=3) {
                    haveSameColor=i+1;
                    break;
                }
            }

            let haveStraigh=-1;//有顺子
            for (let i = 0; i < 3; i++) {
                const card = cardClone[i];
                const cardNext = cardClone[i+1];
                const cardNext2 = cardClone[i+2];
                if ((card.cardNumber+2)==(cardNext.cardNumber+1)&&(card.cardNumber+2)==cardNext2.cardNumber) {
                    haveStraigh=i;
                    break;
                }
            }

            if (haveSameColor!=-1) 
            {
                for (let i = 0; i < 5; i++) {
                    const card = cardClone[i];
                    if (card.cardColor!=haveSameColor) {
                        this.selectCards.push(card);
                    }
                }
            }else if (haveStraigh!=-1) {
                for (let i = 0; i < 5; i++) {
                    const card = cardClone[i];
                    if (i>=haveStraigh&&i<=haveStraigh+2) {
                        
                    }else
                    {
                        this.selectCards.push(card);

                    }
                }
            }else
            {
                if (type==7||type==8)
                    {
                        for (let i = 0; i < cardNum.length; i++) {
                            if (cardNum[i]==1) {
                                for (let j = 0; j < cardClone.length; j++) {
                                    if (cardClone[j].cardNumber==(i+1)) {
                                        this.selectCards.push(cardClone[j]);
                                    }
                                }
                            }
                        }
                    }else
                    {
                        for (let i = 0; i < 5; i++) {
                            this.selectCards.push(cardClone[i]);
                        }
                    }
            }  
        }

        this.scheduleOnce(()=>{
            this.SelectInterchangeCard();
        },0.5);
        this.scheduleOnce(()=>{
            SoltaireGame.instance.NaxtTakeTurnsPlayCard(this.groupId,0.2);
        },1.5);
    }

    //ai 换牌后选择出分
    AiSelectChuScore2()
    {
        this.selectCards=[];
        let type= this.GetPokerHands();

        if (type<=5) 
        {//牌好加分
            this.scheduleOnce(()=>{
                if (GameAllData.Instance.lastPlayUpScore==0) {
                    this.SelectAdd100(200*GameManager.Instance.difficult);
                } else {
                    this.SelectAdd100(GameAllData.Instance.lastPlayUpScore+100*GameManager.Instance.difficult);
                }
            },0.5);
        }else if (type<=7)
        {//三条两队跟分
            this.scheduleOnce(()=>{
                if (GameAllData.Instance.lastPlayUpScore==0) {
                    this.SelectGen(100*GameManager.Instance.difficult);
                } else {
                    this.SelectGen(GameAllData.Instance.lastPlayUpScore);
                }
            },0.5);
        }else if (type<=8)
        {
            this.scheduleOnce(()=>{
                let rd= randomRangeInt(0,100);
                if (rd<50) {
                    if (GameAllData.Instance.lastPlayUpScore==0) {
                        this.SelectGen(100*GameManager.Instance.difficult);
                    } else {
                        this.SelectGen(GameAllData.Instance.lastPlayUpScore);
                    }
                }else
                {
                    this.SelectPass();
                }
            },0.5);
        }else
        {
            this.scheduleOnce(()=>{
                this.SelectPass();
            },0.5);
        }

        this.scheduleOnce(()=>{
            SoltaireGame.instance.NaxtTakeTurnsPlayCard(this.groupId,0.2);
        },1.5);
    }
}




